package ORM;

import test_try.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBC {
    public List<User> findAll(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<User> userList = new ArrayList<>();
        try{
            //注册驱动
            Class.forName("cpm.mysql.cj.jdbc.Driver");
            //获取数据库连接对象
            String url = "jdbc:mysql://localhost:3306/thinkersredata?serverTimezone=GMT&useSSL=false";
            String username = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url,username,password);
            //获取数据库操作对象
            String sql = "???";
            preparedStatement = connection.prepareStatement(sql);
            //执行sql语句
            resultSet = preparedStatement.executeQuery();
            //处理查询结果集
            while(resultSet.next()){
                User user = new User();
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");

                user.setId(id);

                userList.add(user);
            }
            return  userList;
        }
        catch(SQLException e){
            e.printStackTrace();
        }
        finally {
            //释放资源
            try{
                if(resultSet!=null){
                    resultSet.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
